axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net';


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    if (config.url.indexOf('/my/') > -1) {
       
        config.headers.Authorization = localStorage.getItem('token') || ''
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});



// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // 登陆拦截
    if(response.data.message ==="身份认证失败！") {
        localStorage.removeItem('token')
        location.href = '/login.html'
        return
    }
    return response;
  }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
  });


  function formateDate(time) {
      let date = new Date(time)
      let y = date.getFullYear()
      let m = String(date.getMonth()+1).padStart(2,0)
      let d = String(date.getDate()).padStart(2,0)
      let hh = String(date.getHours()).padStart(2,0)
      let mm = String(date.getMinutes()).padStart(2,0)
      let ss = String(date.getSeconds()).padStart(2,0)

      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`

  }